// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   register2_ok.java

package portal.design;

import include.get_rate_from_ID.getRateFromID;
import include.nseer_cookie.*;
import include.nseer_db.nseer_db_backup1;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.jsp.JspFactory;
import validata.ValidataNumber;

public class register2_ok extends HttpServlet
{

	ServletContext application;
	HttpSession session;
	nseer_db_backup1 erp_db;

	public register2_ok()
	{
		erp_db = null;
	}

	public synchronized void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		HttpSession dbSession = request.getSession();
		JspFactory _jspxFactory = JspFactory.getDefaultFactory();
		javax.servlet.jsp.PageContext pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true);
		ServletContext dbApplication = dbSession.getServletContext();
		try
		{
			PrintWriter out = response.getWriter();
			session = request.getSession();
			nseer_db_backup1 crm_db = new nseer_db_backup1(dbApplication);
			if (crm_db.conn((String)dbSession.getAttribute("unit_db_name")))
			{
				counter count = new counter(dbApplication);
				ValidataNumber validata = new ValidataNumber();
				getRateFromID getRateFromID = new getRateFromID();
				String customer_ID = request.getParameter("customer_ID");
				String register_ID = (String)session.getAttribute("human_IDD");
				String product_amount = request.getParameter("product_amount");
				int num = Integer.parseInt(product_amount);
				String demand_pay_fee_sum2 = request.getParameter("demand_pay_fee_sum");
				String sales_name = request.getParameter("sales_name");
				String sales_ID = request.getParameter("sales_ID");
				double order_discount = getRateFromID.getRateFromID((String)dbSession.getAttribute("unit_db_name"), "security_users", "human_ID", sales_ID, "order_discount");
				double order_discount1 = getRateFromID.getRateFromID((String)dbSession.getAttribute("unit_db_name"), "security_users", "human_ID", register_ID, "order_discount");
				int n = 0;
				for (int i = 1; i < num; i++)
				{
					String tem_amount = (new StringBuilder("amount")).append(i).toString();
					String tem_off_discount = (new StringBuilder("off_discount")).append(i).toString();
					String tem_list_price = (new StringBuilder("list_price")).append(i).toString();
					String amount = request.getParameter(tem_amount);
					String off_discount = request.getParameter(tem_off_discount);
					String list_price2 = request.getParameter(tem_list_price);
					StringTokenizer tokenTO2 = new StringTokenizer(list_price2, ",");
					String list_price;
					String list_price1;
					for (list_price = ""; tokenTO2.hasMoreTokens(); list_price = (new StringBuilder(String.valueOf(list_price))).append(list_price1).toString())
						list_price1 = tokenTO2.nextToken();

					if (!validata.validata(amount) || !validata.validata(off_discount) || !validata.validata(list_price))
						n++;
					else
					if (Double.parseDouble(off_discount) > order_discount && Double.parseDouble(off_discount) > order_discount1)
						n++;
				}

				if (n == 0)
				{
					String time = "";
					Date now = new Date();
					SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
					time = formatter.format(now);
					String discussion_ID = NseerId.getId("crm/discussion", (String)dbSession.getAttribute("unit_db_name"));
					String demand_contact_person_tel = request.getParameter("demand_contact_person_tel");
					String demand_pay_time = request.getParameter("demand_pay_time");
					String register_time = request.getParameter("register_time");
					String register = request.getParameter("register");
					String type = request.getParameter("type");
					String bodyc = new String(request.getParameter("remark").getBytes("UTF-8"), "UTF-8");
					String remark = exchange.toHtml(bodyc);
					try
					{
						String sql3 = (new StringBuilder("select * from crm_file where customer_ID='")).append(customer_ID).append("'").toString();
						ResultSet rs = crm_db.executeQuery(sql3);
						if (rs.next())
						{
							String sql = (new StringBuilder("insert into crm_discussion(discussion_ID,chain_ID,chain_name,customer_ID,customer_name,demand_contact_person_tel,demand_pay_time,sales_name,sales_ID,register_time,type,remark,check_tag,excel_tag) values ('")).append(discussion_ID).append("','").append(rs.getString("chain_ID")).append("','").append(rs.getString("chain_name")).append("','").append(customer_ID).append("','").append(rs.getString("customer_name")).append("','").append(demand_contact_person_tel).append("','").append(demand_pay_time).append("','").append(rs.getString("sales_name")).append("','").append(rs.getString("sales_ID")).append("','").append(register_time).append("','").append(rs.getString("type")).append("','").append(remark).append("','0','2')").toString();
							crm_db.executeUpdate(sql);
						}
						String id = "";
						String sqlb = (new StringBuilder("select id from crm_discussion where discussion_ID='")).append(discussion_ID).append("'").toString();
						rs = crm_db.executeQuery(sqlb);
						if (rs.next())
							id = rs.getString("id");
						double sale_price_sum = 0.0D;
						double cost_price_sum = 0.0D;
						double real_cost_price_sum = 0.0D;
						int pay_amount_sum = 0;
						for (int i = 1; i < num; i++)
						{
							String tem_product_name = (new StringBuilder("product_name")).append(i).toString();
							String tem_product_ID = (new StringBuilder("product_ID")).append(i).toString();
							String tem_product_describe = (new StringBuilder("product_describe")).append(i).toString();
							String tem_amount = (new StringBuilder("amount")).append(i).toString();
							String tem_off_discount = (new StringBuilder("off_discount")).append(i).toString();
							String tem_list_price = (new StringBuilder("list_price")).append(i).toString();
							String tem_cost_price = (new StringBuilder("cost_price")).append(i).toString();
							String tem_real_cost_price = (new StringBuilder("real_cost_price")).append(i).toString();
							String tem_amount_unit = (new StringBuilder("amount_unit")).append(i).toString();
							String product_name = request.getParameter(tem_product_name);
							String product_ID = request.getParameter(tem_product_ID);
							String product_describe = request.getParameter(tem_product_describe);
							String amount = request.getParameter(tem_amount);
							String amount_unit = request.getParameter(tem_amount_unit);
							String off_discount = request.getParameter(tem_off_discount);
							String list_price2 = request.getParameter(tem_list_price);
							StringTokenizer tokenTO2 = new StringTokenizer(list_price2, ",");
							String list_price;
							String list_price1;
							for (list_price = ""; tokenTO2.hasMoreTokens(); list_price = (new StringBuilder(String.valueOf(list_price))).append(list_price1).toString())
								list_price1 = tokenTO2.nextToken();

							String cost_price2 = request.getParameter(tem_cost_price);
							StringTokenizer tokenTO3 = new StringTokenizer(cost_price2, ",");
							String cost_price;
							String cost_price1;
							for (cost_price = ""; tokenTO3.hasMoreTokens(); cost_price = (new StringBuilder(String.valueOf(cost_price))).append(cost_price1).toString())
								cost_price1 = tokenTO3.nextToken();

							String real_cost_price2 = request.getParameter(tem_real_cost_price);
							StringTokenizer tokenTO4 = new StringTokenizer(real_cost_price2, ",");
							String real_cost_price;
							String real_cost_price1;
							for (real_cost_price = ""; tokenTO4.hasMoreTokens(); real_cost_price = (new StringBuilder(String.valueOf(real_cost_price))).append(real_cost_price1).toString())
								real_cost_price1 = tokenTO4.nextToken();

							double subtotal = Double.parseDouble(list_price) * (1.0D - Double.parseDouble(off_discount) / 100D) * Double.parseDouble(amount);
							double cost_price_after_discount_sum = Double.parseDouble(cost_price) * Double.parseDouble(amount);
							double real_cost_price_after_discount_sum = Double.parseDouble(real_cost_price) * Double.parseDouble(amount);
							sale_price_sum += subtotal;
							cost_price_sum += cost_price_after_discount_sum;
							real_cost_price_sum += real_cost_price_after_discount_sum;
							pay_amount_sum = (int)((double)pay_amount_sum + Double.parseDouble(amount));
							String sql1 = (new StringBuilder("insert into crm_discussion_details(discussion_ID,details_number,product_ID,product_name,product_describe,list_price,amount,cost_price,real_cost_price,off_discount,subtotal,amount_unit) values ('")).append(discussion_ID).append("','").append(i).append("','").append(product_ID).append("','").append(product_name).append("','").append(product_describe).append("','").append(list_price).append("','").append(amount).append("','").append(cost_price).append("','").append(real_cost_price).append("','").append(off_discount).append("','").append(subtotal).append("','").append(amount_unit).append("')").toString();
							crm_db.executeUpdate(sql1);
						}

						List rsList = GetWorkflow.getList(crm_db, "crm_config_workflow", "02");
						String elem[] = new String[3];
						if (rsList.size() == 0)
						{
							String sql = (new StringBuilder("update crm_discussion set sale_price_sum='")).append(sale_price_sum).append("',cost_price_sum='").append(cost_price_sum).append("',real_cost_price_sum='").append(real_cost_price_sum).append("',check_tag='1' where discussion_ID='").append(discussion_ID).append("'").toString();
							crm_db.executeUpdate(sql);
						} else
						{
							String sql = (new StringBuilder("update crm_discussion set sale_price_sum='")).append(sale_price_sum).append("',cost_price_sum='").append(cost_price_sum).append("',real_cost_price_sum='").append(real_cost_price_sum).append("' where discussion_ID='").append(discussion_ID).append("'").toString();
							crm_db.executeUpdate(sql);
							for (Iterator ite = rsList.iterator(); ite.hasNext(); crm_db.executeUpdate(sql))
							{
								elem = (String[])ite.next();
								sql = (new StringBuilder("insert into crm_workflow(config_id,object_ID,describe1,describe2) values ('")).append(elem[0]).append("','").append(discussion_ID).append("','").append(elem[1]).append("','").append(elem[2]).append("')").toString();
							}

						}
						response.sendRedirect((new StringBuilder("portal/design/register2_choose_attachment.jsp?discussion_ID=")).append(discussion_ID).toString());
					}
					catch (Exception ex)
					{
						out.println((new StringBuilder("error")).append(ex).toString());
					}
				} else
				{
					response.sendRedirect((new StringBuilder("portal/design/register2_ok_a.jsp?customer_ID=")).append(customer_ID).toString());
				}
				crm_db.commit();
				crm_db.close();
			} else
			{
				response.sendRedirect("error_conn.htm");
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
